<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加品牌</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <div style="width: 600px; margin: 0;
  height: 250px">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
        <el-form-item label="名称" prop="name">
          <el-input  v-model="ruleForm.name"></el-input>
        </el-form-item>

        <el-form-item label="拼音" prop="pinyin">
          <el-input v-model="ruleForm.pinyin"></el-input>
        </el-form-item>

        <el-form-item label="LOGO" prop="logo">
          <el-input v-model="ruleForm.logo"></el-input>
        </el-form-item>

        <el-form-item label="简介" prop="description">
          <el-input v-model="ruleForm.description"></el-input>
        </el-form-item>

        <el-form-item label="关键词" prop="keywords">
          <el-input v-model="ruleForm.keywords"></el-input>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input v-model="ruleForm.sort"></el-input>
        </el-form-item>

        <el-form-item label="是否启用">
          <el-switch
              v-model="ruleForm.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#cccccc">
          </el-switch>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        name: 0,
        pinyin: '测试类别大大',
        logo: 'https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png',
        description: 'asdasd',
        keywords: 22,
        sort:1,
        enable:1
      },
      rules: {
        pinyin: [
          {required: true, message: '请输入拼音', trigger: 'blur'},
          {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '简介', trigger: 'blur'},
          {min: 1, max: 15, message: '长度在 1 到 15个字符', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入品牌名称', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
        ],
        keywords: [
          {required: true, message: '请输入类别关键词列表', trigger: 'blur'},
          {min: 2, max: 255, message: '长度在 2 到 255 个字符', trigger: 'blur'}
        ],
        logo: [
          {required: true, message: '请输入品牌图标的URL', trigger: 'blur'},
          {min: 10, max: 255, message: '长度在 10 到 255 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入相册排序序号', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '相册排序序号必须是0~99之间的数字', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:8080/brand/add-new";
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log(formData);

          this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state === 20000) {
              this.$message.success("添加品牌成功");
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>
body {
  background: #2c3e50;
}

.el-input {
  width: 1000px;
}
</style>